﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using SerialozLibrary;
using SSS.AlphaNN.Logic;
//using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using SerialozLibrary;
namespace SSS.AlphaNN.Components
{
    /// <summary>
    /// 
    /// </summary>
    public enum WorkGroundState
    {
        /// <summary>
        /// 
        /// </summary>
        Normal,
        /// <summary>
        /// 
        /// </summary>
        InsertGizmo
    }

    public partial class WorkGround:UserControl
    {
        /// <summary>
        /// Конструктор
        /// </summary>
        public WorkGround()
        {
            InitializeComponent();
            FileName = String.Empty;
            this.DoubleBuffered = true;
            //Dummy.privateIndex = 0;
            BackColor = Color.WhiteSmoke;
            //openFromFile=false;
        }

        /*
         * Props
         * ======================
         */

        /// <summary>
        /// 
        /// </summary>
        private bool shift_key_pressed = false;

        /// <summary>
        /// 
        /// </summary>
        public bool IsShiftPressed
        {
            get
            {
                return shift_key_pressed;
            }
            set
            {
                shift_key_pressed = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private float _zoom = 0;

        /// <summary>
        /// 
        /// </summary>
        public float Zoom
        {
            get
            {
                return _zoom;
            }
            set
            {
                _zoom = value;
            }
        }

        /*
         * Fields
         * ===============================
         */

        /// <summary>
        /// 
        /// </summary>
        private List<Link> links = new List<Link>();

        /// <summary>
        /// 
        /// </summary>
        public System.Collections.Generic.Stack<Slot> markedSlots = new Stack<Slot>( 2 );

        /// <summary>
        /// 
        /// </summary>
        private WorkGroundState _wrkGroundState = WorkGroundState.Normal;

        /// <summary>
        /// 
        /// </summary>
        private Type _dummyTypeToInsert = null;

        /// <summary>
        /// 
        /// </summary>
        private Point selRectCorner;

        /// <summary>
        /// 
        /// </summary>
        private Point md;

        /// <summary>
        /// 
        /// </summary>
        private List<object> selected = new List<object>();

        /// <summary>
        /// 
        /// </summary>
        private List<Dummy> dummies = new List<Dummy>();

        /*
         * Methods
         * ============================
         */

        public void Scale( int side )
        {
            //float pred = _zoom;
            //_zoom = ( side > 0 ) ? _zoom + 10F : _zoom - 10F;

            //if ( _zoom < -50 ) _zoom = -50;
            //if ( _zoom > 0 ) _zoom = 0;

            for ( int i = 0; i < dummies.Count; i++ )
            {


                // dummies[i].Dummy_Paint( null, new PaintEventArgs( dummy.CreateGraphics(), new Rectangle( 0, 0, dummy.Width, dummy.Height ) ) );

                dummies[i].Scale();
                dummies[i].ReLocate();

            }

            this.Focus();
            this.Refresh();
        }


        public void ShowAnimate()
        {
            for ( int i = 0; i < dummies.Count; i++ )
            {

                dummies[i].DoAnimate();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void MakeLink()
        {
            Slot o2 = (Slot)markedSlots.Pop();
            Slot o1 = (Slot)markedSlots.Pop();

            o1.Mark = false;
            o2.Mark = false;

            links.Add( new Link( o1, o2 ) );

            RemoveMarkFromAllObjs( true );

            Invalidate();
        }

        ///// <summary>
        ///// 
        ///// </summary>
        //public void DrawLinks()
        //{
        //    Graphics e = Graphics.FromHwnd( this.Handle );
        //    e.Clear( this.BackColor );

        //    Point p1, p2;

        //    foreach ( Link link in links )
        //    {
        //        p1 = new Point( link.Slot1.Left + ( ( link.Slot1.Left != 0 ) ? link.Slot1.Left + link.Slot1.Width : link.Slot1.Left ), link.Slot1.Top + link.Slot1.Top + link.Slot1.Height / 2 );
        //        p2 = new Point( link.Slot2.Left + ( ( link.Slot2.Left != 0 ) ? link.Slot2.Left + link.Slot2.Width : link.Slot2.Left ), link.Slot2.Top + link.Slot2.Top + link.Slot2.Height / 2 );

        //        e.DrawLine( new Pen( link.LinkColor ), p1, p2 );
        //    }
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dummyInsertionType"></param>
        public void BeginInsertion( Type dummyInsertionType )
        {
            if ( _wrkGroundState == WorkGroundState.Normal )
            {
                _wrkGroundState = WorkGroundState.InsertGizmo;
            }

            _dummyTypeToInsert = dummyInsertionType;
        }

        /// <summary>
        /// 
        /// </summary>
        public void EndInsertion()
        {
            _wrkGroundState = WorkGroundState.Normal;
            _dummyTypeToInsert = null;
        }

        /// <summary>
        /// 
        /// </summary>
        public void RemoveMarkFromAllObjs( bool forceUnmark )
        {
            for ( int dummy_counter = 0; dummy_counter < dummies.Count; dummy_counter++ )
            {
                dummies[dummy_counter].HideSlots();
                dummies[dummy_counter].Mark = false;

                for ( int slot_counter = 0; slot_counter < dummies[dummy_counter].Slots.Count; slot_counter++ )
                {
                    if ( ( dummies[dummy_counter].Slots[slot_counter].SlotState == SlotStateEnum.Marked ) )
                    {
                        if ( forceUnmark )
                        {
                            dummies[dummy_counter].Slots[slot_counter].Visible = false;
                            dummies[dummy_counter].Slots[slot_counter].SlotState = SlotStateEnum.None;

                            dummies[dummy_counter].Slots[slot_counter].Mark = false;
                        }
                    }
                }
            }

            if ( forceUnmark )
            {
                markedSlots.Clear();
            }

            for ( int link_counter = 0; link_counter < links.Count; link_counter++ )
            {
                links[link_counter].Mark = false;
            }

            markedDummies_list.Clear();
            markedLinks_list.Clear();

            this.Invalidate();
        }

        List<Dummy> markedDummies_list = new List<Dummy>();
        List<Link> markedLinks_list = new List<Link>();

        /// <summary>
        /// 
        /// </summary>
        public void EraseMarkedObjs()
        {
            for ( int dummy_counter = 0; dummy_counter < markedDummies_list.Count; dummy_counter++ )
            {
                for ( int link_counter = 0; link_counter < links.Count; link_counter++ )
                {
                    for ( int slot_counter = 0; slot_counter < markedDummies_list[dummy_counter].Slots.Count; slot_counter++ )
                    {
                        if ( ( links[link_counter].Slot1 == markedDummies_list[dummy_counter].Slots[slot_counter] )
                          || ( links[link_counter].Slot2 == markedDummies_list[dummy_counter].Slots[slot_counter] )
                          || ( links[link_counter].Mark )
                           )
                        {
                            markedLinks_list.Add( links[link_counter] );
                        }
                    }
                }
            }


            for ( int dummy_counter = 0; dummy_counter < markedDummies_list.Count; dummy_counter++ )
            {
                if ( markedDummies_list[dummy_counter] is Source )
                {
                    source_sink_teacher[0] = 0;
                }
                else if ( markedDummies_list[dummy_counter] is Sink )
                {
                    source_sink_teacher[1] = 0;
                }
                else if ( markedDummies_list[dummy_counter] is Teacher )
                {
                    source_sink_teacher[2] = 0;
                }

                for ( int slot_counter = 0; slot_counter < markedDummies_list[dummy_counter].Slots.Count; slot_counter++ )
                {
                    this.Controls.Remove( markedDummies_list[dummy_counter].Slots[slot_counter] );
                }

                markedDummies_list[dummy_counter].Slots.Clear();

                dummies.Remove( markedDummies_list[dummy_counter] );

                this.Controls.Remove( markedDummies_list[dummy_counter] );
            }

            for ( int link_counter = 0; link_counter < markedLinks_list.Count; link_counter++ )
            {
                links.Remove( markedLinks_list[link_counter] );
            }

            selected_dummy = null;

            markedDummies_list.Clear();
            markedLinks_list.Clear();
            markedSlots.Clear();

            this.Invalidate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dummy"></param>
        private void ShowPropWin( Dummy dummy )
        {
            ShowDialogProperties( dummy );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="link"></param>
        private void ShowPropWin( Link link )
        {
            ShowDialogForLink( link );
        }




        /*
         * Event handlers
         * =============================
         */

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void WorkGround_Paint( object sender, PaintEventArgs e )
        {
            e.Graphics.Clear( this.BackColor );

            for ( int i = 0; i < dummies.Count; i++ )
            {
                dummies[i].ReLocate();

                if ( dummies[i] is Neuron )
                {

                    e.Graphics.FillEllipse( new Pen( dummies[i].current_color ).Brush, dummies[i].Location.X, dummies[i].Location.Y, dummies[i].Width, dummies[i].Height );

                    e.Graphics.DrawEllipse( ( ( dummies[i].Mark )?dummies[i].MarkedBorderColor:Pens.Black ), dummies[i].Location.X, dummies[i].Location.Y, dummies[i].Width, dummies[i].Height );

                }
                else if ( ( dummies[i] is Source ) || ( dummies[i] is Sink ) )
                {
                    e.Graphics.FillRectangle( Brushes.Azure, dummies[i].Location.X, dummies[i].Location.Y, dummies[i].Width, dummies[i].Height );
                    e.Graphics.DrawRectangle( ( ( dummies[i].Mark ) ? dummies[i].MarkedBorderColor : Pens.Black ), dummies[i].Location.X, dummies[i].Location.Y, dummies[i].Width, dummies[i].Height );
                }
                else if ( dummies[i] is Teacher )
                {
                    e.Graphics.FillRectangle( Brushes.Azure, dummies[i].Location.X, dummies[i].Location.Y, dummies[i].Width, dummies[i].Height );
                    e.Graphics.DrawRectangle( ( ( dummies[i].Mark ) ? dummies[i].MarkedBorderColor : Pens.Black ), dummies[i].Location.X, dummies[i].Location.Y, dummies[i].Width, dummies[i].Height );
                }

            }

            //( new Pen( Color.FromArgb( 0xFF, Color.FromArgb( 0x99FFCC ) ) ) ).Brush

            Point p1 = new Point(), p2 = new Point();

            foreach ( Link link in links )
            {
                //p1 = new Point( link.Slot1.Left + link.Slot1.Width / 2, link.Slot1.Top + link.Slot1.Height / 2 );
                //p2 = new Point( link.Slot2.Left + link.Slot2.Width / 2, link.Slot2.Top + link.Slot2.Height / 2 );

                if ( link.Slot1.Name.Contains( "slot1" ) )
                {
                    p1 = new Point( link.Slot1.Left, link.Slot1.Top + link.Slot1.Height / 2 );
                }
                else if ( link.Slot1.Name.Contains( "slot2" ) )
                {
                    p1 = new Point( link.Slot1.Left + link.Slot1.Width, link.Slot1.Top + link.Slot1.Height / 2 );
                }

                if ( link.Slot2.Name.Contains( "slot1" ) )
                {
                    p2 = new Point( link.Slot2.Left, link.Slot2.Top + link.Slot2.Height / 2 );
                }
                else if ( link.Slot2.Name.Contains( "slot2" ) )
                {
                    p2 = new Point( link.Slot2.Left + link.Slot2.Width, link.Slot2.Top + link.Slot2.Height / 2 );
                }

                e.Graphics.DrawLine( new Pen( link.LinkColor ), p1, p2 );
            }

        }

        private byte[] source_sink_teacher = new byte[3];

        /// <summary>
        /// 
        /// </summary>
        private Dummy selected_dummy;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WorkGround_MouseDown( object sender, MouseEventArgs e )
        {


            switch ( e.Button )
            {
                case MouseButtons.Left:

                if ( _wrkGroundState == WorkGroundState.InsertGizmo )
                {
                    if ( ( ( _dummyTypeToInsert == typeof( Source ) ) && ( source_sink_teacher[0] == 1 ) ) ||
                             ( ( ( _dummyTypeToInsert == typeof( Sink ) ) && ( source_sink_teacher[1] == 1 ) ) ) ||
                             ( ( ( _dummyTypeToInsert == typeof( Teacher ) ) && ( source_sink_teacher[2] == 1 ) ) )
                       )
                    {
                        return;
                    }



                    Dummy dummy = (Dummy)Activator.CreateInstance( _dummyTypeToInsert, this );

                    // Это мой кусок кода Савиных АС
                    AddObjectInList( dummy );
                    //конец листинга;-)

                    //dummy.Parent = this;
                    this.Controls.Add( dummy );

                    //dummy.NoneScaleLocation = new Point( ( e.X + ( int )( e.X * ( -_zoom * 2 ) / 100 ) ) - ( dummy.Width + ( int )( dummy.Width * _zoom / 100 ) ) / 2, ( e.Y + ( int )( e.Y * ( -_zoom * 2 ) / 100 ) ) - ( dummy.Height + ( int )( dummy.Height * _zoom / 100 ) ) / 2 );

                    dummy.NoneScaleLocation = new Point( e.X + (int)( e.X * ( -Zoom ) * 2 / 100 ) - dummy.Width / 2, e.Y + (int)( e.Y * ( -Zoom ) * 2 / 100 ) - dummy.Height / 2 );
                    //dummy.NoneScaleLocation = new Point( dummy.NoneScaleLocation.X - dummy.Width / 2, dummy.NoneScaleLocation.Y - dummy.Height / 2 );

                    dummy.CreateSlots();

                    //dummy.Location = new Point( e.X - dummy.Width / 2, e.Y - dummy.Height / 2 );

                    dummy.Scale();

                    dummy.ReLocate();

                    //Array.Resize<Dummy>( ref dummies, dummies.Length + 1 );
                    //dummies[dummies.Length - 1] = dummy;
                    dummies.Add( dummy );



                    if ( _dummyTypeToInsert == typeof( Source ) )
                    {
                        source_sink_teacher[0] = 1;
                    }
                    else if ( _dummyTypeToInsert == typeof( Sink ) )
                    {
                        source_sink_teacher[1] = 1;
                    }
                    else if ( _dummyTypeToInsert == typeof( Teacher ) )
                    {
                        source_sink_teacher[2] = 1;
                    }

                    break;
                }

                // link clicking
                //======================================================

                PointF p1, p2;

                foreach ( Link link in links )
                {
                    p1 = new PointF( link.Slot1.Left + link.Slot1.Width / 2, link.Slot1.Top + link.Slot1.Height / 2 );
                    p2 = new PointF( link.Slot2.Left + link.Slot2.Width / 2, link.Slot2.Top + link.Slot2.Height / 2 );

                    if ( p1.X > p2.X )
                    {
                        PointF p_tmp = p1;
                        p1 = p2;
                        p2 = p_tmp;
                    }

                    double len1 = Math.Sqrt( Math.Pow( p2.X - p1.X, 2 ) + Math.Pow( p2.Y - p1.Y, 2 ) );
                    double len2 = Math.Sqrt( Math.Pow( p1.X - e.X, 2 ) + Math.Pow( p1.Y - e.Y, 2 ) );
                    double len3 = Math.Sqrt( Math.Pow( p2.X - e.X, 2 ) + Math.Pow( p2.Y - e.Y, 2 ) );

                    if ( Math.Abs( ( len2 + len3 ) - len1 ) < 0.2 )
                    {
                        if ( !shift_key_pressed )
                        {
                            RemoveMarkFromAllObjs( false );
                        }

                        link.Mark = true;

                        markedLinks_list.Add( link );

                        if ( e.Clicks == 2 )
                        {
                            ShowPropWin( link );
                        }

                        break;
                    }

                }


                // Dummy clicking
                //===========================================================================================================
                for ( int i = 0; i < dummies.Count; i++ )
                {
                    if ( ( ( e.X >= dummies[i].Location.X ) && ( e.X <= ( dummies[i].Location.X + dummies[i].Width ) ) ) &&
                             ( ( e.Y >= dummies[i].Location.Y ) && ( e.Y <= ( dummies[i].Location.Y + dummies[i].Height ) ) )
                       )
                    {

                        if ( !shift_key_pressed )
                        {
                            RemoveMarkFromAllObjs( false );
                        }

                        dummies[i].Mark = true;

                        markedDummies_list.Add( dummies[i] );


                        for ( int k = 0; k < dummies[i].Slots.Count; k++ )
                        {
                            if ( !( dummies[i].Slots[k].SlotState == SlotStateEnum.Marked ) )
                            {
                                dummies[i].Slots[k].Visible = true;
                                dummies[i].Slots[k].SlotState = SlotStateEnum.Selected;
                            }
                        }




                        md = e.Location;
                        selected_dummy = dummies[i];
                        selected_dummy.TemporalPoint = selected_dummy.NoneScaleLocation;

                        center_offset.X = ( selected_dummy.Left + selected_dummy.Width / 2 ) - md.X;
                        center_offset.Y = ( selected_dummy.Top + selected_dummy.Height / 2 ) - md.Y;


                    }
                    else
                    {
                        for ( int k = 0; k < dummies[i].Slots.Count; k++ )
                        {
                            if ( !( dummies[i].Slots[k].SlotState == SlotStateEnum.Marked ) )
                            {
                                dummies[i].Slots[k].Visible = false;
                            }
                        }
                    }
                }

                break;


                case MouseButtons.Right:

                md = e.Location;

                for ( int i = 0; i < dummies.Count; i++ )
                {
                    dummies[i].HideSlots();
                    dummies[i].TemporalPoint = dummies[i].NoneScaleLocation;


                    if ( ( ( e.X >= dummies[i].Location.X ) && ( e.X <= ( dummies[i].Location.X + dummies[i].Width ) ) ) &&
                             ( ( e.Y >= dummies[i].Location.Y ) && ( e.Y <= ( dummies[i].Location.Y + dummies[i].Height ) ) )
                       )
                    {
                        ShowPropWin( dummies[i] );

                        break;
                    }
                }

                break;
            }


            this.Refresh();
        }



        bool drifting = false;

        Point center_offset = new Point();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WorkGround_MouseMove( object sender, MouseEventArgs e )
        {


            switch ( e.Button )
            {
                case MouseButtons.Left:



                if ( selected_dummy != null )
                {
                    if ( !drifting )
                    {
                        drifting = true;

                        for ( int i = 0; i < selected_dummy.Slots.Count; i++ )
                        {
                            selected_dummy.Slots[i].Visible = false;
                        }
                    }

                    selected_dummy.NoneScaleLocation = new Point( e.X + (int)( e.X * ( -Zoom ) * 2 / 100 ) - selected_dummy.Width / 2, e.Y + (int)( e.Y * ( -Zoom ) * 2 / 100 ) - selected_dummy.Height / 2 );
                    //selected_dummy.Location = new Point( e.X - selected_dummy.Width / 2, e.Y - selected_dummy.Height / 2 );

                    selected_dummy.NoneScaleLocation = new Point( selected_dummy.NoneScaleLocation.X + center_offset.X, selected_dummy.NoneScaleLocation.Y + center_offset.Y );

                    selected_dummy.Scale();
                    selected_dummy.ReLocate();

                    this.Refresh();
                }

                break;

                case MouseButtons.Right:



                for ( int i = 0; i < dummies.Count; i++ )
                {
                    dummies[i].NoneScaleLocation = new Point( dummies[i].TemporalPoint.X + ( e.Location.X - md.X ), dummies[i].TemporalPoint.Y + ( e.Location.Y - md.Y ) );
                    //dummies[i].Location = new Point( dummies[i].TemporalPoint.X + ( e.Location.X - md.X ), dummies[i].TemporalPoint.Y + ( e.Location.Y - md.Y ) );
                    dummies[i].Scale();
                    dummies[i].ReLocate();
                }


                this.Invalidate();

                break;

            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WorkGround_MouseUp( object sender, MouseEventArgs e )
        {
            if ( selected_dummy != null )
            {
                if ( drifting )
                {
                    drifting = false;

                    for ( int i = 0; i < selected_dummy.Slots.Count; i++ )
                    {
                        selected_dummy.Slots[i].Visible = true;
                    }
                }


                selected_dummy = null;
                
            }

            for ( int i = 0; i < dummies.Count; i++ )
            {
                for ( int k = 0; k < dummies[i].Slots.Count; k++ )
                {
                    if ( dummies[i].Slots[k].SlotState == SlotStateEnum.Marked )
                        dummies[i].Slots[k].Visible = true;
                }
            }
        }



        public void DumpGhost( string fname )
        {
            ser_tmp_ canvas = new ser_tmp_();

            canvas.dummies = new DummyGhost[dummies.Count];
            for ( int i = 0; i < dummies.Count; i++ )
            {
                canvas.dummies[i] = dummies[i].GetGhost( dummies[i].GetType() );

            }

            canvas.links = new LinkGhost[links.Count];
            for ( int i = 0; i < links.Count; i++ )
            {
                canvas.links[i] = links[i].GetGhost();
            }


            BinaryFormatter bf = new BinaryFormatter();
            bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
            FileStream fs = new FileStream( fname, FileMode.Create );

            bf.Serialize( fs, canvas );
            fs.Close();
        }

        /// <summary>
        /// Загрузка из файла
        /// </summary>
        /// <param name="fname"></param>
        public void LoadGhostDump( string fname )
        {
            
            FileStream fs = File.Open( fname, FileMode.Open );
            LoadGhostDump(fs);
           

        }

        /// <summary>
        /// Загрузка из потока
        /// </summary>
        /// <param name="fs"></param>
        public void LoadGhostDump(Stream fs)
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
            dummies.Clear();
            links.Clear();

            ser_tmp_ deccepted_obj = ((ser_tmp_)bf.Deserialize(fs));

            foreach (DummyGhost dummyGhost in deccepted_obj.dummies)
            {

                Dummy dummy = (Dummy)Activator.CreateInstance(dummyGhost.dummyType, this);


                if (dummyGhost.dummyType == typeof(Source))
                {
                    source_sink_teacher[0] = 1;
                    Source ss = (Source)dummy;

                    ss.PathFileDb = Convert.ToString(dummyGhost.data[0]);
                    ss.Login = Convert.ToString(dummyGhost.data[1]);
                    ss.Query = Convert.ToString(dummyGhost.data[2]);
                    ss.countSignal = Convert.ToInt32(dummyGhost.data[3]);
                }
                else if (dummyGhost.dummyType == typeof(Sink))
                {
                    source_sink_teacher[1] = 1;

                    Sink ss = (Sink)dummy;
                    ss.PathFileDb = Convert.ToString(dummyGhost.data[0]);
                    ss.Login = Convert.ToString(dummyGhost.data[1]);
                    ss.Query = Convert.ToString(dummyGhost.data[2]);
                    ss.countSignal = Convert.ToInt32(dummyGhost.data[3]);
                    ss.DateAndTime = Convert.ToString(dummyGhost.data[4]);
                    ss.typeFileInput = (TypeFileInput)Convert.ToInt32(dummyGhost.data[5]);
                }
                else if (dummyGhost.dummyType == typeof(Teacher))
                {
                    source_sink_teacher[2] = 1;
                    Teacher ss = (Teacher)dummy;
                    ss.PathFileDb = Convert.ToString(dummyGhost.data[0]);
                    ss.Login = Convert.ToString(dummyGhost.data[1]);
                    ss.Query = Convert.ToString(dummyGhost.data[2]);
                    ss.countSignal = Convert.ToInt32(dummyGhost.data[3]);
                    ss.countLearning = Convert.ToInt32(dummyGhost.data[4]);
                    ss.epsilon = Convert.ToInt32(dummyGhost.data[5]);
                    ss.coefficientLearning = Convert.ToDouble(dummyGhost.data[6]);
                    ss.SpeedLearning = Convert.ToDouble(dummyGhost.data[7]);
                }
                else if (dummyGhost.dummyType == typeof(Neuron))
                {
                    Neuron ss = (Neuron)dummy;
                    ss.Offset = Convert.ToDouble(dummyGhost.data[0]);
                    ss.IdActivationFunction = Convert.ToInt32(dummyGhost.data[1]);
                    for (int i = 2; i < dummyGhost.data.Length; i++)
                    {
                        ss.ParametersForActivationFunction.Add(dummyGhost.data[i]);
                    }
                }


                dummy.Identificator = dummyGhost.dummyID;
                dummy.Parent = this;
                dummy.Visible = false;
                dummy.NoneScaleLocation = dummyGhost.noneScaleLocation;

                dummy.CreateSlots();
                dummy.Scale();

                dummy.ReLocate();





                dummies.Add(dummy);
                this.Refresh();
            }



            foreach (LinkGhost linkGhost in deccepted_obj.links)
            {
                Slot slot1 = null, slot2 = null;
                foreach (Dummy dummy in dummies)
                {

                    if ((dummy.Identificator == linkGhost.dummy1_id) || (dummy.Identificator == linkGhost.dummy2_id))
                    {
                        for (int i = 0; i < dummy.Slots.Count; i++)
                        {
                            if ((dummy.Identificator == linkGhost.dummy1_id) && (dummy.Slots[i].Name == linkGhost.slot1_name.ToString()))
                            {
                                slot1 = dummy.Slots[i];
                            }
                            if ((dummy.Identificator == linkGhost.dummy2_id) && (dummy.Slots[i].Name == linkGhost.slot2_name.ToString()))
                            {
                                slot2 = dummy.Slots[i];
                            }
                        }
                    }
                }

                if ((slot1 != null) && (slot2 != null))
                {
                    links.Add(new Link(slot1, slot2)
                    {
                        ValueLink = linkGhost.value
                    });
                }
            }


            fs.Close();
        }

        private void WorkGround_Click(object sender, EventArgs e)
        {
            
           
        }



    }


}
